package edu.columbia.cloudbox.ui;

import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import edu.columbia.cloudbox.CloudBoxActivity;
import edu.columbia.cloudbox.UserStatusService;
import edu.columbia.cloudbox.io.FileType;
import edu.columbia.cloudbox.io.S3IO;
import edu.columbia.cloudbox.policy.CBDBHelper;

public class AllFilesActivity extends ListActivity {

	private static final String LOG_TAG = "AllFilesActivity";

	private static List<FileType> files;
	
	private CBDBHelper dbHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		dbHelper = new CBDBHelper(this);
		
		setBackButton();
		browse(UserStatusService.USER_NAME, "");
	}

	@Override
	protected void onResume() {
		super.onResume();

		String path = FilePathOnTab
				.makePath(FilePathOnTab.currentPath_allFiles);
		setBackButton();

		browse(UserStatusService.USER_NAME, path);

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.i(LOG_TAG, "Item clicked at " + position);

		super.onListItemClick(l, v, position, id);

		boolean isFolder = (Boolean) v.getTag();

		if (isFolder) {

			RelativeLayout layout = (RelativeLayout) v;
			TextView textView = (TextView) layout.getChildAt(1);
			String folderName = textView.getText().toString();
			// Update current path
			FilePathOnTab.currentPath_allFiles.push(folderName);
			String path = FilePathOnTab
					.makePath(FilePathOnTab.currentPath_allFiles);
			browse(UserStatusService.USER_NAME, path);
		}
	}
	
	@Override
	protected void onDestroy() {

		super.onDestroy();
		dbHelper.close();
	}

	/**
	 * Display all files as mirror of S3 storage
	 * 
	 * @return
	 */
	private void browse(String username, String dir) {
		String methodName = "browse(" + username + dir + ")";
		Log.i(LOG_TAG, methodName + ": entering...");

		CloudBoxActivity.pathTextView.setText(dir);

		S3IO s3io = UserStatusService.S3IO_INSTANCE;
		String folder = username;
		if (!"".equals(dir)) {
			folder = username + dir;
			CloudBoxActivity.backButton.setVisibility(View.VISIBLE);
		} else {
			CloudBoxActivity.backButton.setVisibility(View.INVISIBLE);
		}

		Log.i(LOG_TAG, methodName + ": folder path: " + folder);
		files = s3io.getFileTypesByFolder(folder);

		Log.i(LOG_TAG, methodName + ": Files: " + files);
		AllFilesAdapter filesAdapter = new AllFilesAdapter(this, files);
		this.setListAdapter(filesAdapter);
	}

	private void setBackButton() {
		CloudBoxActivity.backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String backPath = FilePathOnTab
						.makeBackPath(FilePathOnTab.currentPath_allFiles);
				Log.i(LOG_TAG, "Back path: " + backPath);
				browse(UserStatusService.USER_NAME, backPath);
			}

		});
	}
}
